#include "headfiles.h"

int firstMissingPositive(int *a, int n) {
    if (a == NULL ||
        n <= 0) {
        std::cout << "Invalid argument!" << std::endl;
        return 1;
    }

    int i;
    for (i = 0; i < n; i++) {
        while (a[i] >= 0 && a[i] < n && a[i] != a[a[i] - 1]) {
            int tmp = a[a[i] - 1];
            a[a[i] - 1] = a[i];
            a[i] = tmp;
        }
    }

    for (i = 0; i < n; i++) {
        if (a[i] != i + 1) {
            return i + 1;
        }
    }

    return n + 1;
}
